include(${CMAKE_SOURCE_DIR}/cmake/options/options_definitions.cmake)
# 生成配置头文件的函数，添加项目子目录后再用
function(generate_config_header)
    # 确定输出路径
    set(OUTPUT_DIR "${CMAKE_SOURCE_DIR}/Projects/common")
    file(MAKE_DIRECTORY ${OUTPUT_DIR})
    set(HEADER_FILE "${OUTPUT_DIR}/project_config.h")

    # 创建配置内容
    set(CONFIG_CONTENT "#pragma once\n")
    string(APPEND CONFIG_CONTENT "// ============================================================================\n")
    string(APPEND CONFIG_CONTENT "// 自动生成的配置头文件 - ${PROJECT_NAME}\n")
    string(APPEND CONFIG_CONTENT "// 请勿手动编辑此文件，它由 CMake 自动生成\n")
    string(APPEND CONFIG_CONTENT "// ============================================================================\n\n")

    # 添加时间戳（使用安全格式）
    string(TIMESTAMP TIMESTAMP "%Y-%m-%d_%H-%M-%S")  # 使用下划线代替冒号
    string(APPEND CONFIG_CONTENT "// 生成时间: ${TIMESTAMP}\n\n")

    # 添加功能选项
    string(APPEND CONFIG_CONTENT "// ================= 功能选项 =================\n")

    # 首先获取所有选项状态
    foreach(opt_desc IN LISTS OPTION_DEFINITIONS)
        string(REGEX REPLACE "^([^:]+):(.+)$" "\\1;\\2" parts "${opt_desc}")
        list(GET parts 0 opt_id)
        list(GET parts 1 opt_desc_text)
        string(TOUPPER ${opt_id} opt_upper)

        # 检查变量是否在父作用域定义
        if(DEFINED ENABLE_${opt_upper})
            set(option_value ${ENABLE_${opt_upper}})
        else()
            set(option_value OFF)
        endif()
#        message(STATUS "[${opt_id}]: ${option_value}")

        # 添加到内容
        string(APPEND CONFIG_CONTENT "// ${opt_desc_text}\n")
        if(option_value)
            string(APPEND CONFIG_CONTENT "#define ENABLE_${opt_upper}\n\n")
        else()
            string(APPEND CONFIG_CONTENT "// #define ENABLE_${opt_upper}\n\n")
        endif()
    endforeach()

    # 写入文件
    file(WRITE ${HEADER_FILE} "${CONFIG_CONTENT}")
    message(STATUS "Generate ${HEADER_FILE}")
endfunction()